package com.sl.transport.info.service.impl;

import com.google.common.collect.Lists;

import cn.hutool.core.util.ObjectUtil;
import com.sl.transport.info.config.RedisConfig;
import com.sl.transport.info.entity.TransportInfoDetail;
import com.sl.transport.info.entity.TransportInfoEntity;
import com.sl.transport.info.service.BloomFilterService;
import com.sl.transport.info.service.TransportInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class TransportInfoServiceImpl implements TransportInfoService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private BloomFilterService bloomFilterService;


    @Resource
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 如果运单数据不存在，就新增，否则更新数据
     *
     * @param transportOrderId 运单id
     * @param infoDetail       信息详情
     * @return
     */
    // #p0这个就是表示方法上面的参数  第一个参数
    @CachePut(value = "transport-info", key = "#p0") //更新缓存数据
    @Override
    public TransportInfoEntity saveOrUpdate(String transportOrderId, TransportInfoDetail infoDetail) {
        // 1.创建查询条件
        Query query = Query.query(Criteria.where("transportOrderId").is(transportOrderId));

        TransportInfoEntity transportInfoEntity = mongoTemplate.findOne(query, TransportInfoEntity.class);


        if (transportInfoEntity == null || ObjectUtil.isEmpty(transportInfoEntity)) {
            // 新增
            transportInfoEntity = new TransportInfoEntity();
            transportInfoEntity.setTransportOrderId(transportOrderId);
            transportInfoEntity.setInfoList(Lists.newArrayList(infoDetail));
            transportInfoEntity.setCreated(System.currentTimeMillis());
        } else {
            // 修改
            transportInfoEntity.getInfoList().add(infoDetail);
        }
        transportInfoEntity.setUpdated(System.currentTimeMillis());

        // 删除caffeine里面的数据
        //transportInfoCache.invalidate(transportOrderId);

        // 使用redis的发布订阅来实现
        // 发消息
        stringRedisTemplate.convertAndSend(RedisConfig.CHANNEL_TOPIC, transportOrderId);

        bloomFilterService.add(transportOrderId);

        //保存/更新到MongoDB
        return this.mongoTemplate.save(transportInfoEntity);
    }

    /**
     * 根据运单id查询运输信息
     *
     * @param transportOrderId 运单id
     * @return
     */
    // #p0这个就是表示方法上面的参数  第一个参数
    @Cacheable(value = "transport-info", key = "#p0") //新增缓存数据
    @Override
    public TransportInfoEntity queryByTransportOrderId(String transportOrderId) {
        Query query = Query.query(Criteria.where("transportOrderId").is(transportOrderId));
        return mongoTemplate.findOne(query, TransportInfoEntity.class);
    }
}
